package com.bs.service.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    public static final String DAY="d";
    public static final String MONTH="m";
	public static Timestamp getTime(){
		Date date=new Date();
		Timestamp timestamp=new Timestamp(date.getTime());
		return timestamp;
	}
	public static Timestamp getBeforeDay(String unit,int number){
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(new Date());
        switch (unit){
            case DAY:
                calendar.add(Calendar.DATE, -number);  //设置为前几天
                break;
            case MONTH:
                calendar.add(Calendar.MONTH, -number);  //设置为前几个月
        }
        Date date=calendar.getTime();
        Timestamp timestamp=new Timestamp(date.getTime());
        return timestamp;
    }

    public static Date stringToDate(String str) {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
}
